package com.dcy.znoa.controller.estate;

import com.aliyuncs.exceptions.ClientException;
import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.enums.ResultCode;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.estate.model.advice.AddEstateAdviceModel;
import com.dcy.znoa.controller.estate.model.advice.SelectEstateAdviceModel;
import com.dcy.znoa.domain.estate.EstateAdvice;
import com.dcy.znoa.service.estate.EstateAdviceService;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import static com.dcy.znoa.common.util.pojo.JsonCommon.*;

/**
 * <p>
 * 物业建议 前端控制器
 * </p>
 *
 * @author cook
 * @since 2018-05-21
 */
@Controller
@RequestMapping("/estateAdvice")
public class EstateAdviceController extends BaseController {

    @Autowired
    EstateAdviceService eaService;
    //列表查询 分页  通用Controller
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectEstateAdviceModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonForList("404", bindingResult.getFieldError().getDefaultMessage());
        }
        return eaService.selectList(model);
    }

    //APP
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddEstateAdviceModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        EstateAdvice cp = new EstateAdvice();
        BeanUtils.copyProperties(cp, model);
        cp.setCreateOperId(getCurrentUser().getOperId());
        cp.setSendOperId(getCurrentUser().getOperId());
        cp.setCompId(getCurrentUser().getCompId());
        cp.setCreateTime(new Date());
        cp.setValidFlag(1);
        boolean result = eaService.add(cp);
        if(result){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(INSERT_ERROR_CODE);
        }
    }

    //WEB 逻辑删除
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDelById")
    public JsonCommon delById(Long esadId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (esadId==null){
            return new JsonCommon(ResultCode.MISS_PARAM.getCode(), ResultCode.MISS_PARAM.getMessage());
        }
        EstateAdvice cp = eaService.selectById(esadId);
        if(cp==null){
            return  new JsonCommon(NOT_FOUND_CODE);
        }
        cp.setValidFlag(0);
        boolean result = cp.updateById();
        if(result){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(DELETE_ERROR_CODE);
        }
    }

    
}
